//
//  HomePage.swift
//  Keep1
//
//  Created by 王志涛 on 2021/3/1.
//

import SwiftUI

struct HomePage: View {
    private let backgroundColor = Color(.sRGB, red: 150/255, green: 150/255, blue: 150/255, opacity: 0.1)
    private let purple = Color(.sRGB, red: 103/255, green: 58/255, blue: 183/255)
    private let orange = Color(.sRGB, red: 255/255, green: 156/255, blue: 0/255)
    
    var body: some View {
        
        ZStack{
            Color.init(.white)
                .ignoresSafeArea(.all)
            VStack{
                HomeTopBar()
                    .background(Color.white)
                ScrollView(showsIndicators: false) {
                    CategoryBoard()
                        .padding(.top)
                    HStack{
                        Text("分类")
                            .font(.title2)
                            .bold()
                            .padding()
                        Spacer()
                    }
                    VStack(spacing: 20){
                        CategoryCard(titleColor: purple, titleName: "课程分类")
                        CategoryCard(titleColor: orange, titleName: "目标分类")
                    }
                }
                .background(Color.init(hex: "#F9F9F9"))
            }
        }
        
    }
}

struct HomePage_Previews: PreviewProvider {
    static var previews: some View {
        HomePage()
    }
}

extension Color {
    static let cloloABC = Color(hex: "#FFFFFF", alpha: 0.5)
    init(hex: String, alpha: CGFloat = 1.0) {
        var hex = hex.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
        if hex.hasPrefix("#") {
            hex = String(hex.dropFirst())
        }
        assert(hex.count == 3 || hex.count == 6 || hex.count == 8, "Invalid hex code used. hex count is #(3, 6, 8).")
        var int: UInt64 = 0
        Scanner(string: hex).scanHexInt64(&int)
        let r, g, b: UInt64
        switch hex.count {
        case 3: // RGB (12-bit)
            (r, g, b) = ((int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
        case 6: // RGB (24-bit)
            (r, g, b) = (int >> 16, int >> 8 & 0xFF, int & 0xFF)
        case 8: // ARGB (32-bit)
            (r, g, b) = (int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
        default:
            (r, g, b) = (1, 1, 0)
        }

        self.init(
            .sRGB,
            red: Double(r) / 255,
            green: Double(g) / 255,
            blue:  Double(b) / 255,
            opacity: Double(alpha)
        )
    }
}
